function buildSearchPart(name, value) {
	prefix = "+(@\\{vgr.model\\}";

	if (value == "" || value == "*") {
		//return "" + prefix + name + ":*" + "";
		return " ";
	} else {
		return "" + prefix + name + ":\"" + value + "\") ";
	}
}

savedSearch = null;
// Finds the saved search that has been specified in the URL.
if (args.name != undefined) {
	name =  "+@\\{vgr.model\\}listName:\"" + args.name + "\"";
	path = "((PATH:\"/app:company_home/cm:vgr/cm:Saved_x0020_Searches//*\") AND ";
	type = "(TYPE:\"{vgr.model}savedSearch\"))";
	
	savedSearchResult = search.luceneSearch(name + path + type);
	
	if (savedSearchResult[0] != undefined) {
		savedSearch = savedSearchResult[0];
	} else {
		status.code = 404;
		status.message = "Den sparade s&ouml;kningen: " + args.name + " hittades inte.";
		status.redirect = true;
	}
} else {
	 status.code = 404;
	 status.message = "Den sparade s&ouml;kningen: " + args.name + " hittades inte.";
	 status.redirect = true;
}

//Metadata about the list. Do we need it here?
listName = savedSearch.properties["vgr:listName"];
listDescription = savedSearch.properties["vgr:listDescription"];
listAdministration = savedSearch.properties["vgr:listAdministration"];
listMajorArea = savedSearch.properties["vgr:listMajorArea"];

model.savedSearch = savedSearch;

//Metadata about the search to perform
//model.searchCriterias = savedSearch.properties["vgr:searchCriterias"];
searchCriterias = savedSearch.properties["vgr:searchCriterias"];

searchParts = new Array();
for each(criteria in searchCriterias) {
	keyAndValue = criteria.split(":");
	key = keyAndValue[0];
	value = keyAndValue[1];
	
	if (key == "search-doctype") searchParts.push(buildSearchPart("docType", value));
	if (key == "search-doctitle") searchParts.push(buildSearchPart("docTitle", value)); 
	if (key == "search-docid") searchParts.push(buildSearchPart("docId", value));
	if (key == "search-doccreatedby") searchParts.push(buildSearchPart("docCreatedBy", value));
	if (key == "search-docdescription") searchParts.push(buildSearchPart("docDescription", value)); 
	
	if (key == "search-createdbyauthor") searchParts.push(buildSearchPart("createdByAuthor", value));
	if (key == "search-createdbyadministration") searchParts.push(buildSearchPart("createdByAdministration", value));
	if (key == "search-createdbyunit") searchParts.push(buildSearchPart("createdByUnit", value));
	
	if (key == "search-responsiblename") searchParts.push(buildSearchPart("responsibleName", value)); 
	if (key == "search-responsibleadministration") searchParts.push(buildSearchPart("responsibleAdministration", value));
	if (key == "search-responsibleunit") searchParts.push(buildSearchPart("responsibleUnit", value));
	
	if (key == "search-businessmajorarea") searchParts.push(buildSearchPart("businessMajorArea", value));
	if (key == "search-businessminorarea") searchParts.push(buildSearchPart("businessMinorArea", value));
	if (key == "search-businessproject") searchParts.push(buildSearchPart("businessProject", value));
	if (key == "search-businessassigner") searchParts.push(buildSearchPart("businessAssigner", value));
	
	if (key == "search-referencedocumentsuite") searchParts.push(buildSearchPart("referenceDocumentSuite", value));
	if (key == "search-referencenumber") searchParts.push(buildSearchPart("referenceNumber", value));
}

// Metadata about presentation
model.showAllVersions = savedSearch.properties["vgr:showAllVersions"];
showColumns = savedSearch.properties["vgr:showColumns"];
model.showIcon = savedSearch.properties["vgr:showIcon"];
model.showDescription = savedSearch.properties["vgr:showDescription"];

//Sorting
model.showDescSort = savedSearch.properties["vgr:showDescSort"];
model.showSortBy = savedSearch.properties["vgr:showSortBy"];

//Showable columns
model.showDocId = false;
model.showDocType = false;
model.showVersion = false;
model.showCreatedByAuthor = false;
model.showResponsibleName = false;
model.showPublishedAt = false;

for each(column in showColumns) {
	if (column == "vgr:docId") model.showDocId = true;
	if (column == "vgr:docType") model.showDocType = true;
	if (column == "cm:versionLabel") model.showVersion = true;
	if (column == "vgr:createdByAuthor") model.showCreatedByAuthor = true;
	if (column == "vgr:responsibleName") model.showResponsibleName = true;
	if (column == "vgr:publishedAt") model.showPublishedAt = true;
}

// TODO: Do we need to use this?
searchAccessZone = savedSearch.properties["vgr:searchAccessZone"];

// build the query
query = "";
for each(searchPart in searchParts) {
	query += searchPart + " ";
}

query += " +(ASPECT:\"{vgr.model}webbdok\") +(PATH:\"/app:company_home/cm:vgr/cm:published//*\")"
//model.nodes = search.luceneSearch(query);

if (model.showAllVersions) {
	nodes = search.luceneSearch(query);
	versionNodes = new Array();
	for each(node in nodes) {
		versionHistory = node.versionHistory;
		if (versionHistory != null) {
			for (i = 0; i < versionHistory.length; i++) {
               version = versionHistory[i];
               versionNodes.push(version.node);
            }
		} else {
			versionNodes.push(node);
		}
	}
	model.nodes = versionNodes;
} else {
	model.nodes = search.luceneSearch(query);
}